package com.ruoyi.shopping.service.impl;

import java.util.List;

import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shopping.domain.MallOrder;
import com.ruoyi.shopping.service.IMallOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shopping.mapper.MallOrderMeshMapper;
import com.ruoyi.shopping.domain.MallOrderMesh;
import com.ruoyi.shopping.service.IMallOrderMeshService;

/**
 * 商城订单合并下单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-09
 */
@Service
public class MallOrderMeshServiceImpl implements IMallOrderMeshService
{
    @Autowired
    private MallOrderMeshMapper mallOrderMeshMapper;
    @Autowired
    private IMallOrderService orderService;

    /**
     * 查询商城订单合并下单
     *
     * @param id 商城订单合并下单主键
     * @return 商城订单合并下单
     */
    @Override
    public MallOrderMesh selectMallOrderMeshById(Long id)
    {
        return mallOrderMeshMapper.selectMallOrderMeshById(id);
    }

    /**
     * 查询商城订单合并下单列表
     *
     * @param mallOrderMesh 商城订单合并下单
     * @return 商城订单合并下单
     */
    @Override
    public List<MallOrderMesh> selectMallOrderMeshList(MallOrderMesh mallOrderMesh)
    {
        return mallOrderMeshMapper.selectMallOrderMeshList(mallOrderMesh);
    }

    /**
     * 新增商城订单合并下单
     *
     * @param mallOrderMesh 商城订单合并下单
     * @return 结果
     */
    @Override
    public int insertMallOrderMesh(MallOrderMesh mallOrderMesh)
    {
        mallOrderMesh.setCreateTime(DateUtils.getNowDate());
        return mallOrderMeshMapper.insertMallOrderMesh(mallOrderMesh);
    }

    /**
     * 修改商城订单合并下单
     *
     * @param mallOrderMesh 商城订单合并下单
     * @return 结果
     */
    @Override
    public int updateMallOrderMesh(MallOrderMesh mallOrderMesh)
    {
        mallOrderMesh.setUpdateTime(DateUtils.getNowDate());
        return mallOrderMeshMapper.updateMallOrderMesh(mallOrderMesh);
    }

    /**
     * 批量删除商城订单合并下单
     *
     * @param ids 需要删除的商城订单合并下单主键
     * @return 结果
     */
    @Override
    public int deleteMallOrderMeshByIds(Long[] ids)
    {
        return mallOrderMeshMapper.deleteMallOrderMeshByIds(ids);
    }


    /**
     * 批量逻辑删除商城订单合并下单
     *
     * @param ids 需要删除的商城订单合并下单主键
     * @return 结果
     */
    @Override
    public int logicDeleteMallOrderMesh(Long[] ids,String delType,Long delUser)
    {
        return mallOrderMeshMapper.logicDeleteMallOrderMesh(ids,delType,delUser);
    }

    /**
     * 删除商城订单合并下单信息
     *
     * @param id 商城订单合并下单主键
     * @return 结果
     */
    @Override
    public int deleteMallOrderMeshById(Long id)
    {
        return mallOrderMeshMapper.deleteMallOrderMeshById(id);
    }

    @Override
    public MallOrderMesh selectByOrderId(String verifyCode) {
        return mallOrderMeshMapper.selectByOrderId(verifyCode);
    }

    //根据id批量查询
    @Override
    public List<MallOrderMesh> selectMallOrderMeshByIds(List<Long> mallordeIds) {
        return mallOrderMeshMapper.selectMallOrderMeshByIds(mallordeIds) ;
    }

    @Override
    public List<MallOrderMesh> selectNeedWithDraw() {
        return mallOrderMeshMapper.selectNeedWithDraw();
    }

    @Override
    public void cancelOrder(Long id) {
        MallOrderMesh mesh = this.selectMallOrderMeshById(id);
        mesh.setOrderState(DicConstants.ORDER_STATE_FINISHED.getCode());
        List<MallOrder> orders = orderService.selectMallOrderByMeshId(mesh.getId());
        orders.forEach(e->{
            e.setOrderState(DicConstants.ORDER_STATE_FINISHED.getCode());
            orderService.updateMallOrder(e);
        });
        this.updateMallOrderMesh(mesh);
    }
}
